//
//  LinkTestCtr.swift
//  sinaDemo
//
//  Created by Xueming on 2021/4/27.
//

import UIKit
import RxCocoa
import RxSwift
class LinkTestCtr: UIViewController {
    let disposeBag = DisposeBag()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setUpUI()
        // Do any additional setup after loading the view.
    }
    deinit {
        print("LinkTestCtr deinit")
    }
    private func setUpUI(){
        let printView:UITextView = UITextView.init(frame: CGRect(x: 10, y: 200 + 20, width: 400, height: 200))
        printView.textColor = .black
        printView.text = "输出台"
        printView.font = UIFont.systemFont(ofSize: 12)
        printView.isEditable = false
        printView.backgroundColor = .orange
        view.addSubview(printView)
       
        let btn = UIButton.init(frame: CGRect(x: 20, y: kNavigationHeight + 10, width: 100, height: 20))
        btn.setTitle("单链表", for: .normal)
        btn.setTitleColor(.black, for: .normal)
        btn.rx.controlEvent(.touchUpInside).subscribe(onNext:{[weak self] in
           printView.text = self?.testSingleLinkedList() ?? ""
        }).disposed(by: self.disposeBag)
        view.addSubview(btn)
        
        let btn1 = UIButton.init(frame: CGRect(x: 20, y: btn.bottom + 20, width: 100, height: 20))
        btn1.setTitle("双链表", for: .normal)
        btn1.setTitleColor(.black, for: .normal)
        btn1.rx.controlEvent(.touchUpInside).subscribe(onNext:{[weak self] in
            printView.text = self?.testDoubleLinkedList() ?? ""
        }).disposed(by: self.disposeBag)
        view.addSubview(btn1)
        
        let btn3 = UIButton.init(frame: CGRect(x: btn.right + 10, y: btn.top, width: btn.width + 50, height: btn.height))
        btn3.setTitle("单链表反转(递归)", for: .normal)
        btn3.setTitleColor(.black, for: .normal)
        btn3.rx.controlEvent(.touchUpInside).subscribe(onNext:{[weak self] in
            printView.text =  self?.reverSingleList()
        }).disposed(by: self.disposeBag)
        view.addSubview(btn3)

    }

}
// MARK: - 链表测试
private extension LinkTestCtr {
    ///反转单链表
    func reverSingleList()-> String{
        
        let list = SingleLinkedList<Int>()
        
        list.append(11)
        list.append(22)
        list.append(33)
        list.append(44)

        list.insert(0, element: 55); // [55, 11, 22, 33, 44]
        list.insert(2, element: 66); // [55, 11, 66, 22, 33, 44]
        list.insert(list.count(), element: 77); // [55, 11, 66, 22, 33, 44, 77]

        _ = list.remove(0) // [11, 66, 22, 33, 44, 77]
        _ = list.remove(2) // [11, 66, 33, 44, 77]
        _ = list.remove(list.count() - 1) // [11, 66, 33, 44]

        assert(list.indexOf(44) == 3)
        assert(list.indexOf(22) == nil)
        assert(list.contains(33))
        assert(list.get(0) == 11)
        assert(list.get(1) == 66)
        assert(list.get(list.count() - 1) == 44)
      
        list.rever()
        print(list.description)
        return list.description
    }
    /// 测试单向链表
    func testSingleLinkedList() -> String{
        let list = SingleLinkedList<Int>()
        
        list.append(11)
        list.append(22)
        list.append(33)
        list.append(44)

        list.insert(0, element: 55); // [55, 11, 22, 33, 44]
        list.insert(2, element: 66); // [55, 11, 66, 22, 33, 44]
        list.insert(list.count(), element: 77); // [55, 11, 66, 22, 33, 44, 77]

        _ = list.remove(0) // [11, 66, 22, 33, 44, 77]
        _ = list.remove(2) // [11, 66, 33, 44, 77]
        _ = list.remove(list.count() - 1) // [11, 66, 33, 44]

        assert(list.indexOf(44) == 3)
        assert(list.indexOf(22) == nil)
        assert(list.contains(33))
        assert(list.get(0) == 11)
        assert(list.get(1) == 66)
        assert(list.get(list.count() - 1) == 44)
        
        print(list.description)
        return list.description
    }
    
    /// 测试双向链表
    func testDoubleLinkedList() -> String {
        
        let list = DoubleLinkedList<Int>()
        
        list.append(11)
        list.append(22)
        list.append(33)
        list.append(44)

        list.insert(0, element: 55); // [55, 11, 22, 33, 44]
        list.insert(2, element: 66); // [55, 11, 66, 22, 33, 44]
        list.insert(list.count(), element: 77); // [55, 11, 66, 22, 33, 44, 77]

        _ = list.remove(0) // [11, 66, 22, 33, 44, 77]
        _ = list.remove(2) // [11, 66, 33, 44, 77]
        _ = list.remove(list.count() - 1) // [11, 66, 33, 44]

        assert(list.indexOf(44) == 3)
        assert(list.indexOf(22) == nil)
        assert(list.contains(33))
        assert(list.get(0) == 11)
        assert(list.get(1) == 66)
        assert(list.get(list.count() - 1) == 44)
        
        print(list.description)
        return list.description
    }
}
